﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseData;

namespace ZBJF.Warranty.AdminSite.Controllers
{
    public class D_ProvinceController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        #region 获取省份列表 JsonResult Index(D_ProvinceModel model)
        [HttpPost]
        public JsonResult Index(D_ProvinceModel model)
        {
            var total = 0;
            var list = ServiceFactory.D_ProvinceServiceInstance.GetPagingList(model, out total).AsNoTracking().ToList().Select(o => new
            {
                o.ProvinceID,
                o.Sort,
                o.ProvinceName,
                o.IsAvailable
            });
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
         #endregion
        public ActionResult Edit(int id)
        {
            var productBrand = ServiceFactory.D_ProvinceServiceInstance.LoadByID(id);
            return View(productBrand);
        }
        #region 保存省份信息 ActionResult SaveProvince(D_Province model)
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SaveProvince(D_Province model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ProvinceID > 0 ? OpreateType.Update : OpreateType.Add
            };
            if (model.ProvinceID == 0)
            {
                if (ServiceFactory.D_ProvinceServiceInstance.IsExist(model.ProvinceName))
                {
                    responseResult.Message = "该省份已经存在！";
                }
                else
                {
                    var entity = ServiceFactory.D_ProvinceServiceInstance.AddEntities(model);
                    responseResult.Success = entity.ProvinceID > 0;
                }
            }
            else
            {
                var entity = ServiceFactory.D_ProvinceServiceInstance.LoadByID(model.ProvinceID);
                if (entity != null)
                {
                    if (ServiceFactory.D_ProvinceServiceInstance.IsExist(model.ProvinceName, model.ProvinceID))
                    {
                        responseResult.Message = "已存在同名的省份，请修改省份名！";
                    }
                    entity.ProvinceName = model.ProvinceName;
                    entity.Sort = model.Sort;
                    entity.IsAvailable = model.IsAvailable;
                    responseResult.Success = ServiceFactory.D_ProvinceServiceInstance.UpdateEntities(entity);
                }
                else
                {
                    responseResult.Message = "该省份不存在或已被删除！";
                }
            }
            TempData["ResponseResult"] = responseResult;
            return responseResult.OpreateType == OpreateType.Add ? RedirectToAction("Index") : RedirectToAction("Edit", new { id = model.ProvinceID });
        }
         #endregion
        #region  修改状态  JsonResult UpdateStatus(int id, bool isAvailable)
        [HttpPost]
        public JsonResult UpdateStatus(int id, bool isAvailable)
        {
            var entity = ServiceFactory.D_ProvinceServiceInstance.LoadByID(id);
            var success = false;
            if (entity != null)
            {
                entity.IsAvailable = isAvailable;
                success = ServiceFactory.D_ProvinceServiceInstance.UpdateEntities(entity);
            }
            return Json(new
            {
                success = success,
                message = success ? "修改成功" : "修改失败"
            });
        }
       #endregion
    }
}